package mmc.objects;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.opengl.GL11;

import mmc.Constants;
import mmc.EngineObject;
import mmc.MovableEngineObject;
import mmc.Settings;
import mmc.physics.PhysicsReactionType;
import mmc.physics.PhysicsType;
import mmc.physics.Position;
import mmc.resource.Texture;
import mmc.resource.TextureLoader;
import mmc.server.Scene;
import mmc.server.Server;

@SuppressWarnings("serial")
public class Bomb extends MovableEngineObject{
	
	static Texture tex;
	
	static{
		if(!Constants.IS_SERVER){
			tex = TextureLoader.load("mmc/resource/data/Bomb.png");
		}
	}

	private transient double timer = Settings.BOMB_LIFETIME;
	private transient boolean exploded = false;
	
	public Bomb(float x, float y, float z, float vx, float vy, float vz) {
		super(x, y, z, vx, vy, vz);
		physicsType = PhysicsType.MANAGED;
		reactionType = PhysicsReactionType.BOUNCE;
		boundingSphere.radius = 0.5f;
	}

	@Override
	public void render(EngineObject observer) {
		glPushMatrix();
		
		glTranslatef(x,y,z);
		glScalef(0.2f,0.2f,0.2f);
		tex.bind();
		
		
		GL11.glBegin(GL11.GL_QUADS); // Start Drawing Quads
		// Front Face
		GL11.glNormal3f(0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Point 1 (Front)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Point 2 (Front)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Point 3 (Front)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Point 4 (Front)
		// Back Face
		GL11.glNormal3f(0.0f, 0.0f, -1.0f); // Normal Pointing Away From Viewer
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Back)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Point 2 (Back)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Point 3 (Back)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Point 4 (Back)
		// Top Face
		GL11.glNormal3f(0.0f, 1.0f, 0.0f); // Normal Pointing Up
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Point 1 (Top)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Point 2 (Top)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Point 3 (Top)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Point 4 (Top)
		// Bottom Face
		GL11.glNormal3f(0.0f, -1.0f, 0.0f); // Normal Pointing Down
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Bottom)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Point 2 (Bottom)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Point 3 (Bottom)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Point 4 (Bottom)
		// Right face
		GL11.glNormal3f(1.0f, 0.0f, 0.0f); // Normal Pointing Right
		glTexCoord2f(0, 0);
		GL11.glVertex3f(1.0f, -1.0f, -1.0f); // Point 1 (Right)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Point 2 (Right)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Point 3 (Right)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(1.0f, -1.0f, 1.0f); // Point 4 (Right)
		// Left Face
		GL11.glNormal3f(-1.0f, 0.0f, 0.0f); // Normal Pointing Left
		glTexCoord2f(0, 0);
		GL11.glVertex3f(-1.0f, -1.0f, -1.0f); // Point 1 (Left)
		glTexCoord2f(1, 0);
		GL11.glVertex3f(-1.0f, -1.0f, 1.0f); // Point 2 (Left)
		glTexCoord2f(1, 1);
		GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Point 3 (Left)
		glTexCoord2f(0, 1);
		GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Point 4 (Left)
		GL11.glEnd(); // Done Drawing Quads
		
		glPopMatrix();
	}

	@Override
	public void update(float dt) {
		if(vy!=0)
			vy += Settings.PHYSICS_GRAVITY*dt;
		
		x += vx*dt;
		y += vy*dt;
		z += vz*dt;
		
		//dirty = true;
	}
	
	@Override
	public void update(Server server, float dt) {
		timer -= dt;
		if(timer<0)
			explode();
	}
	
	@Override
	public void notifyCellCollission(Position pos) {
		//explode();
	}

	@Override
	public void notifyCollission(EngineObject other, Position pos) {
		//explode();
	}

	public void explode(){
		if(exploded)
			return;
		
		exploded = true;
		
		Scene scene = Server.instance.getScene();
		scene.removeObject(this);
		scene.doDmgAOE(this,Settings.BOMB_EXPLOSION_RANGE,0.25f);
	}
}
